/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */
package space.game;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
import java.io.IOException;
import java.net.URL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3bc;
import javax.media.opengl.GLContext;
import space.model.Material;

/**
 *
 * @author Pierre
 */
public class WldMaterial {

    //Material material;
    final Texture texture;
    final Texture normal;

    public WldMaterial(GL3bc gl, Material material) throws IOException {
        if (material.diffuseMap.isEmpty() == false) {
            final String resourcePath = "/res" + material.diffuseMap.substring(1);
            final URL resource = getClass().getResource(resourcePath);
            if (resource == null) {
                throw new IOException("Resource " + resourcePath + " not found.");
            }

            texture = TextureIO.newTexture(resource, true, null);
        } else {
            texture = null;
        }
        if (material.normalMap.isEmpty() == false) {
            final String resourcePath = "/res" + material.normalMap.substring(1);
            final URL resource = getClass().getResource(resourcePath);
            if (resource == null) {
                throw new IOException("Resource " + resourcePath + " not found.");
            }
            normal = TextureIO.newTexture(resource, true, null);


        } else {
            normal = null;
        }
    }

    public WldMaterial(final GL3bc gl, final Material material, URL cl) throws IOException {
        if (material.diffuseMap.isEmpty() == false) {
            final String resourcePath = material.diffuseMap.substring(2);
            final URL resource = new URL(cl,resourcePath);
            if (resource == null) {
                throw new IOException("Diffuse map at '" + resourcePath + "' not found.");
            }
            texture = TextureIO.newTexture(resource, true, null);
        } else {
            texture = null;
        }
        if (material.normalMap.isEmpty() == false) {
            final String resourcePath = material.normalMap.substring(2);
            final URL resource = new URL(cl,resourcePath);
            if (resource == null) {
                throw new IOException("Normal map at '" + resourcePath + "' not found.");
            }
            normal = TextureIO.newTexture(resource, true, null);
        } else {
            normal = null;
        }
    }

    public void draw(GL3bc gl) {
        if (texture != null) {
            gl.glActiveTexture(GL2.GL_TEXTURE0);
            gl.glEnable(gl.GL_TEXTURE_2D);
            gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_REPLACE);
            texture.bind();
        } else {
            gl.glActiveTexture(GL2.GL_TEXTURE0);
            gl.glDisable(GL2.GL_TEXTURE_2D);
        }

        if (normal != null) {
            gl.glActiveTexture(GL2.GL_TEXTURE1);
            gl.glEnable(gl.GL_TEXTURE_2D);
            gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_REPLACE);
            normal.bind();
        } else {
            gl.glActiveTexture(GL2.GL_TEXTURE1);
            gl.glDisable(GL2.GL_TEXTURE_2D);
        }
    }
}
